// Copyright 2011 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//
//  IOSFloatArray.m
//  JreEmulation
//
//  Created by Tom Ball on 6/16/11.
//

#import "IOSFloatArray.h"
#import "IOSPrimitiveClass.h"
//#import "java/lang/Float.h"

@implementation IOSFloatArray

+ (id)arrayWithFloats:(const float *)floats count:(NSUInteger)count {
  return [[[self alloc] initWithFloats:floats
                                 count:count] j2objc_autorelease];
}

- (id)initWithFloats:(const float *)floats count:(NSUInteger)count {
  if ((self = [self initWithLength:count])) {
    if (NULL != floats) {
      memcpy(self.storage.mutableBytes, floats, self.storage.length);
    }
  }
  return self;
}

- (float)floatAtIndex:(NSUInteger)index {
  [self checkIndex:index];
  
  const float *buffer = (const float *)self.storage.bytes;
  return buffer[index];
}

- (float)replaceFloatAtIndex:(NSUInteger)index withFloat:(float)value {
  [self checkIndex:index];
  
  float *buffer = (float *)self.storage.mutableBytes;
  buffer[index] = value;
  return value;
}

- (void)getFloats:(float *)buffer length:(NSUInteger)length {
  [self checkIndex:(length - 1)];
  memcpy(buffer, self.storage.bytes, length * [[self class] elementLength]);
}

- (float)incr:(NSUInteger)index {
  [self checkIndex:index];
  
  float *buffer = (float *)self.storage.mutableBytes;
  return ++buffer[index];
}

- (float)decr:(NSUInteger)index {
  [self checkIndex:index];
  
  float *buffer = (float *)self.storage.mutableBytes;
  return --buffer[index];
}

- (float)postIncr:(NSUInteger)index {
  [self checkIndex:index];
  
  float *buffer = (float *)self.storage.mutableBytes;
  return buffer[index]++;
}

- (float)postDecr:(NSUInteger)index {
  [self checkIndex:index];
  
  float *buffer = (float *)self.storage.mutableBytes;
  return buffer[index]--;
}

- (NSString *)descriptionOfElementAtIndex:(NSUInteger)index {
  return [NSString stringWithFormat:@"%g", [self floatAtIndex:index]];
}

- (IOSClass *)elementType {
  return [[[IOSPrimitiveClass alloc] initWithName:@"float"
                                             type:@"F"] j2objc_autorelease];
}

+ (NSUInteger)elementLength
{
  return sizeof(float);
}

@end
